//
//  ForgotpasswordViewController.m
//  Molian
//
//  Created by 胡津 on 15/4/16.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "ForgotpasswordViewController.h"
#import "RetrievepasswordViewController.h"
#import "MLLoginTextField.h"

@interface ForgotpasswordViewController ()
@property NSInteger countdownTime;
@property (strong, nonatomic) NSTimer *updateCodeTimer;
@property (strong, nonatomic) MLLoginTextField *phonenumber;
@property (strong, nonatomic) BFPaperButton *Forgotpassword;
@property (strong, nonatomic) MLLoginTextField *CodeField;


@end

@implementation ForgotpasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"找回密码";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    self.phonenumber = [[MLLoginTextField alloc]initWithFrame:CGRectMake(0, 80, DeviceWidth, 40)];
    self.phonenumber.placeholder = @"请输入手机号码";
    self.phonenumber.leftPrompt = @"   手机号    ";
    self.phonenumber.keyboardType = UIKeyboardTypeNumberPad;
    self.phonenumber.text = [Utils defaultsForKey:@"accountName"];
    [self.phonenumber style];
    [self.view addSubview:self.phonenumber];
    
    self.CodeField = [[MLLoginTextField alloc]initWithFrame:CGRectMake(0, 130, DeviceWidth, 40)];
    self.CodeField.placeholder = @"输入验证码";
    self.CodeField.leftPrompt = @"   验证码    ";
    self.CodeField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:self.CodeField];
    self.CodeField.toBeNumber = 6;
    [self.CodeField style];
    
    self.Forgotpassword = [[BFPaperButton alloc]initWithFrame:CGRectMake(DeviceWidth-120, 127.5, 100, 35)raised:NO];
    [self.Forgotpassword setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.Forgotpassword setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
    self.Forgotpassword.layer.masksToBounds = YES;//开启圆角
    [self.Forgotpassword.layer setCornerRadius:35/2];
    [self.Forgotpassword addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
    [self.Forgotpassword.layer setBorderColor:APPCOLOR(1) .CGColor];
    [self.Forgotpassword.layer setBorderWidth:1];
    self.Forgotpassword.titleLabel.font = [UIFont fontWithName:FontName size:14];
    [self.view addSubview:self.Forgotpassword];
    
    UILabel *prompt = [[UILabel alloc]initWithFrame:CGRectMake(20, 180, DeviceWidth-40, 20)];
    prompt.text = @" *请输入在口袋恋人注册的账号及收到的验证码";
    prompt.textColor = [UIColor grayColor];
    prompt.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:prompt];
    
    BFPaperButton *Register = [BFPaperButton buttonWithType:UIButtonTypeCustom];
    Register.frame = CGRectMake(20, 210, DeviceWidth-40, 40);
    [Register setTitle:@"下一步" forState:UIControlStateNormal];
    [Register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    Register.layer.masksToBounds = YES;//开启圆角
    [Register setBackgroundImage:[UIImage imageWithColor:APPCOLOR(1)] forState:UIControlStateNormal];
    [Register.layer setCornerRadius:CGRectGetHeight([Register bounds]) / 2];
    Register.titleLabel.font = [UIFont fontWithName:FontName size:14];
    [Register addTarget:self action:@selector(ActionRetrievepassword) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Register];
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnKeyBorad)]];
}

-(void)returnKeyBorad
{
    [self.view endEditing:YES];
}

- (void)getVerifyCode {
    // 获取验证码
    NSString *phoneNum = self.phonenumber.text;
    if (phoneNum&&phoneNum.length>0) {
        [MLUser requestPasswordResetWithPhoneNumber: phoneNum block: ^(BOOL succeeded, NSError *error) {
            [HUD hideFromViewAnimated:YES];
            if (succeeded) {
                [self.CodeField becomeFirstResponder];
                // 获取验证码成功,禁用按钮
                [self.Forgotpassword setEnabled:NO];
                self.countdownTime = 30;
                [self.Forgotpassword setTitle:@"获取验证码" forState:UIControlStateNormal];
                // 启动定时器
                self.updateCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCountdownTime) userInfo:nil repeats:YES];
            } else {
                NSString *msg = @"获取失败,请重试";
                if (AVOS_RES_CODE_PHONE_NOT_EXIST == error.code) {
                    msg = @"手机号码不存在";
                }
                [HUD Prompt:msg ];
            }
        }];
    }else{
         [HUD Prompt:@"请输入手机号码"];
    }
    
   
    [HUD setOnViewWithTitle:nil animated:YES];
}

- (void)updateCountdownTime {
    // 修改获取验证码的倒计时
    self.countdownTime -=  1;
    NSString *time = [NSString stringWithFormat:@"%lds", (long)self.countdownTime];
    [self.Forgotpassword setTitle:time forState:UIControlStateNormal];
    // 处理倒计时结束
    if (0 == self.countdownTime) {
        [self.Forgotpassword setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.Forgotpassword setEnabled:YES];
        [self.updateCodeTimer invalidate];
    }
}


-(void)ActionRetrievepassword
{
    // 指定需要修改密码的手机账号
    NSString *verifyCode = self.CodeField.text;
    if ([verifyCode isEqualToString:@""]) {
        [HUD Prompt:@"请输入验证码"];
        return;
    }
    RetrievepasswordViewController * newPwdView = [[RetrievepasswordViewController alloc] init];
    newPwdView.verifyCode = self.CodeField.text;
    [self.navigationController pushViewController:newPwdView animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}
@end
